package com.example.zjx8969.myapplication

import android.view.View
import android.widget.ImageView
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.appcompat.widget.AppCompatImageView
import androidx.constraintlayout.widget.ConstraintLayout
import com.example.zjx8969.myapplication.view.TitleLayoutView

fun addEditTitleViews(titleLayoutView: TitleLayoutView, clickListener: View.OnClickListener? = null) {
    val itemWidth = DensityUtil.dp2px(50f).toInt()

    /**
     * @param position -1:最左边；0：中间；1:最右边
     */
    fun createAndAddImageView(@IdRes viewId: Int, @DrawableRes resourceId: Int, rightResourceId: Int, position: Int) {
        AppCompatImageView(titleLayoutView.context).apply {
            id = viewId
            if (clickListener != null) setOnClickListener(clickListener)
            setImageResource(resourceId)
            layoutParams = ConstraintLayout.LayoutParams(itemWidth, ConstraintLayout.LayoutParams.WRAP_CONTENT).apply {
                topToTop = ConstraintLayout.LayoutParams.PARENT_ID
                bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
                when (position) {
                    1 -> {
                        endToEnd = rightResourceId
                    }
                    2, 3, 4 -> {
                        endToStart = rightResourceId
                    }

                }

            }
            scaleType = ImageView.ScaleType.CENTER
            titleLayoutView.addView(this)
        }
    }
    createAndAddImageView(R.id.titleRightId1, R.drawable.ic_edit_save, ConstraintLayout.LayoutParams.PARENT_ID, 1)
    createAndAddImageView(R.id.titleRightId2, R.drawable.ic_edit_keypoint, R.id.titleRightId1, 2)
    createAndAddImageView(R.id.titleRightId3, R.drawable.ic_edit_face_select, R.id.titleRightId2, 3)
    createAndAddImageView(R.id.titleRightId4, R.drawable.ic_edit_change_photo, R.id.titleRightId3, 4)

}

fun addCenterView(titleLayoutView: TitleLayoutView, clickListener: View.OnClickListener? = null) {
    val image1 = AppCompatImageView(titleLayoutView.context).apply {
        id = R.id.titleCenterView1
        if (clickListener != null) setOnClickListener(clickListener)
        setImageResource(R.drawable.ic_person_info_email_warning)
        layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT).also {
            it.topToTop = ConstraintLayout.LayoutParams.PARENT_ID
            it.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
            it.startToStart = ConstraintLayout.LayoutParams.PARENT_ID
            it.endToStart = R.id.titleCenterView2
            it.horizontalChainStyle = ConstraintLayout.LayoutParams.CHAIN_PACKED
        }

    }
    titleLayoutView.addView(image1)
    val image2 = AppCompatImageView(titleLayoutView.context).apply {
        id = R.id.titleCenterView2
        if (clickListener != null) setOnClickListener(clickListener)
        setImageResource(R.drawable.ic_notification)
        layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT).also {
            it.topToTop = ConstraintLayout.LayoutParams.PARENT_ID
            it.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
            it.startToEnd = R.id.titleCenterView1
            it.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
        }

    }

    titleLayoutView.addView(image2)
}